package com.newegg.nlp.arangodb;

import java.net.URLEncoder;
import java.util.Optional;
import com.arangodb.springframework.repository.ArangoRepository;

public interface BasicDao<T extends BaseModel> extends ArangoRepository<T, String> {
	
	default String id(String id) {
		try {
			id = id.replace(" ", "_");
			return URLEncoder.encode(id, "UTF-8");
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	default T get(String id) {
		Optional<T> optional = findById(id(id));
		return optional.isPresent() ? optional.get() : null;
	}
	
	default void saveNotExist(T entity) {
		String id = id(entity.getId());
		if(!existsById(id)) {
			entity.setId(id);
			save(entity);
		}
	}
}
